package com.linxz.framework.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.linxz.framework.R;
import com.linxz.framework.utils.WebViewBiz;

/**
 * @author Linxz
 * 创建日期：2019年10月26日 21:17
 * version：v4.5.4
 * 描述：
 */
public class LinxzWebView extends FrameLayout {

    private WebView mWebView;
    private ProgressBar loadingProgress;
    private RelativeLayout layoutError;
    private WebViewBiz webViewBiz;
    private Context context;

    public LinxzWebView(@NonNull Context context) {
        super(context);
    }

    public LinxzWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public LinxzWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        initView();
    }

    private void initView(){
        inflate(context,R.layout.framework_webview,this);
        mWebView=findViewById(R.id.web_View);
        loadingProgress=findViewById(R.id.loadingProgress);
        layoutError=findViewById(R.id.layoutError);
        webViewBiz=WebViewBiz.builder()
                .setWebView(mWebView)
                .setProgressBar(loadingProgress)
                .setOnLoadSuccessListener(new WebViewBiz.OnLoadSuccessListener() {
                    @Override
                    public void onLoadSuccess() {
                        layoutError.setVisibility(View.GONE);
                        mWebView.setVisibility(View.VISIBLE);
                    }
                })
                .setonLoadErrorListener(new WebViewBiz.OnLoadErrorListener() {
                    @Override
                    public void onLoadError() {
                        layoutError.setVisibility(View.VISIBLE);
                        mWebView.setVisibility(View.GONE);
                    }
                })
                .setReceivedTitle(new WebViewBiz.ReceivedTitle() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        if (mReceivedTitle!=null){
                            mReceivedTitle.onReceivedTitle(view,title);
                        }
                    }
                })
                .build();
    }


    @SuppressLint("SetJavaScriptEnabled")
    public void onResume(){
        webViewBiz.onResume();
    }

    public void onPause(){
        webViewBiz.onPause();
    }

    public void onDestroy(){
        webViewBiz.onDestroy();
    }

    public boolean goBack(){
        return webViewBiz.goBack();
    }

    public void loadUrl(String url){
        webViewBiz.loadUrl(url);
    }

    public WebView getWebView(){
        return mWebView;
    }

    private ReceivedTitle mReceivedTitle;
    public void setReceivedTitle(ReceivedTitle receivedTitle){
        this.mReceivedTitle=receivedTitle;
    }
    public interface ReceivedTitle{
        void onReceivedTitle(WebView view, String title);
    }
}
